﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Wozuji.DBUtility;//请先添加引用
namespace Wozuji.DAL
{
    /// <summary>
    /// 数据访问类region_t。
    /// </summary>
    public class region_t
    {
        public region_t()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int region_id)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@region_id", SqlDbType.Int,4)};
            parameters[0].Value = region_id;

            int result = DbHelperSQL.RunProcedure("UP_region_t_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(Wozuji.Model.region_t model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@region_id", SqlDbType.Int,4),
					new SqlParameter("@region_name", SqlDbType.VarChar,50),
					new SqlParameter("@region_code", SqlDbType.NChar,10)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.region_name;
            parameters[2].Value = model.region_code;

            DbHelperSQL.RunProcedure("UP_region_t_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(Wozuji.Model.region_t model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@region_id", SqlDbType.Int,4),
					new SqlParameter("@region_name", SqlDbType.VarChar,50),
					new SqlParameter("@region_code", SqlDbType.NChar,10)};
            parameters[0].Value = model.region_id;
            parameters[1].Value = model.region_name;
            parameters[2].Value = model.region_code;

            DbHelperSQL.RunProcedure("UP_region_t_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int region_id)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@region_id", SqlDbType.Int,4)};
            parameters[0].Value = region_id;

            DbHelperSQL.RunProcedure("UP_region_t_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Wozuji.Model.region_t GetModel(int region_id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@region_id", SqlDbType.Int,4)};
            parameters[0].Value = region_id;

            Wozuji.Model.region_t model = new Wozuji.Model.region_t();
            DataSet ds = DbHelperSQL.RunProcedure("UP_region_t_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["region_id"].ToString() != "")
                {
                    model.region_id = int.Parse(ds.Tables[0].Rows[0]["region_id"].ToString());
                }
                model.region_name = ds.Tables[0].Rows[0]["region_name"].ToString();
                model.region_code = ds.Tables[0].Rows[0]["region_code"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select region_id,region_name,region_code ");
            strSql.Append(" FROM region_t ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" region_id,region_name,region_code ");
            strSql.Append(" FROM region_t ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "region_t";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

